#!/bin/bash
# 奥数学院 - 服务器部署脚本

echo "========================================="
echo "  奥数学院 - 一键部署脚本"
echo "========================================="
echo ""

# 1. 克隆代码
echo "[1/6] 克隆代码..."
mkdir -p /www/wwwroot/www.lycc.top/aoshu
cd /www/wwwroot/www.lycc.top/aoshu
git clone -b main https://gitee.com/banfoguan/aoshu.git .
echo "✓ 代码克隆完成"
echo ""

# 2. 初始化数据库
echo "[2/6] 初始化数据库..."
echo "请输入MySQL root密码："
cat math-academy/database/mysql-init-simple.sql | mysql -u root -p math_academy
echo "✓ 数据库初始化完成"
echo ""

# 3. 创建环境变量
echo "[3/6] 创建环境变量文件..."
read -p "请输入math_academy_user的数据库密码: " db_password

cat > math-academy/.env.production << EOF
DB_HOST=localhost
DB_PORT=3306
DB_USER=math_academy_user
DB_PASSWORD=$db_password
DB_NAME=math_academy
JWT_SECRET=aoshu2025lycc_secret_key_32char
JWT_EXPIRES_IN=86400
NODE_ENV=production
NEXT_PUBLIC_API_URL=https://www.lycc.top/aoshu
PORT=3001
EOF
echo "✓ 环境变量配置完成"
echo ""

# 4. 安装依赖
echo "[4/6] 安装依赖包..."
cd math-academy
npm install
echo "✓ 依赖安装完成"
echo ""

# 5. 构建项目
echo "[5/6] 构建生产版本..."
npm run build
echo "✓ 构建完成"
echo ""

# 6. 启动应用
echo "[6/6] 启动应用..."
npm install -g pm2
pm2 start npm --name "aoshu" -- start
pm2 save
pm2 startup
echo "✓ 应用启动完成"
echo ""

echo "========================================="
echo "  ✓ 部署完成！"
echo "========================================="
echo ""
echo "下一步："
echo "  1. 配置Nginx反向代理（在宝塔面板操作）"
echo "  2. 创建管理员账户: node scripts/create-admin.js"
echo "  3. 访问: https://www.lycc.top/aoshu"
echo ""

